<template>
    <div class="loginbox">
        <div class="login">
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
                class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username" type="text" autocomplete="off" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password" type="text" autocomplete="off" />
                </el-form-item>
                <el-form-item label="验证码" prop="captcha">
                    <el-space>
                        <el-input v-model.number="ruleForm.captcha" />
                        <div @click="changeCaptchaPicFn">
                            <img :src="captchaPic" alt="">
                        </div>
                    </el-space>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                </el-form-item>
            </el-form>
        </div>

    </div>
</template>

<script setup>
import { useRouter } from 'vue-router';
import { getcaptcha, loginApi } from '@/api/login.js'
import { reactive, ref } from 'vue'
const router = useRouter()
const ruleFormRef = ref()
const captchaPic = ref('')
const key = ref('')

//第一次加载验证码图片
getcaptcha().then(res => {
    captchaPic.value = res.data.img
    key.value = res.data.key
})
//切换验证码图片
const changeCaptchaPicFn = () => {
    getcaptcha().then(res => {
        captchaPic.value = res.data.img
        key.value = res.data.key
    })
}

//表单提交的数据
const ruleForm = reactive({
    username: '',
    password: '',
    key: '',
    captcha: ''
})

//表单校验规则
const rules = reactive({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
})

//提交按钮
const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            ruleForm.key = key.value
            //调用登录接口
            loginApi(ruleForm).then(res => {
                //成功登录
                if (res.data.errNo == 0) {
                    console.log('成功', res);
                    localStorage.setItem('jwt', res.data.context.jwt)
                    localStorage.setItem('acl', JSON.stringify(res.data.context.acl))
                    router.push('/dashboard/welcome')
                }
            })
            //登录失败
        } else {
            return false
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style scoped lang="scss">
.loginbox {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .login {
        width: 400px;
    }
    .el-button {
        width: 100%;
    }
}
</style>